﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Drawing;
using System.Drawing.Drawing2D;
using AnguaRPGEngine.Library.Systems;

namespace AnguaRPGEngine.Library.Dice
{
    public class D12 : IDie
    {
        #region ctor
        public D12()
        {
        }

        #endregion
        #region IDie Members
        #region Image
        public System.Drawing.Bitmap Image
        {
            get
            {
                Font frontFirstDigitFont = new Font(FontFamily.GenericSansSerif, 20f);
                Font frontSmallerFont = new Font(FontFamily.GenericSansSerif, 16f);
                Font sideFont = new Font(FontFamily.GenericSansSerif, 12f);
                Font smallerSideFont = new Font(FontFamily.GenericSansSerif, 10f);
                Font bottomFont = new Font(FontFamily.GenericSansSerif, 8f);
                Font smallerBottomFont = new Font(FontFamily.GenericSansSerif, 6f);
                Bitmap i = ImageLibrary.Instance.GetImageFromLibrary(string.Format("Dice.{0}.D{1}", DieRoller.Instance.DiceCollection.CurrentSet.PathName, Sides, _Value));
                using (Graphics g = Graphics.FromImage(i))
                {


                    if (_Value == 6 || _Value == 9)
                    {
                        g.DrawLine(new Pen(new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), 1f), new Point(32, 49), new Point(39, 49));
                    }
                    if (SelectSideToTheUpperLeft(_Value) == "6" || SelectSideToTheUpperLeft(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(28, 13), new Point(26, 8));
                    }
                    if (SelectSideToTheUpperRight(_Value) == "6" || SelectSideToTheUpperRight(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(55, 22), new Point(59, 19));
                    }
                    if (SelectSideBelow(_Value) == "6" || SelectSideBelow(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(43, 62), new Point(41, 60));
                    }
                    if (SelectSideToTheLowerRight(_Value) == "6" || SelectSideToTheLowerRight(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(58, 52), new Point(55, 50));
                    }
                    if (SelectSideToTheLowerLeft(_Value) == "6" || SelectSideToTheLowerLeft(_Value) == "9")
                    {
                        g.DrawLine(new Pen(new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), 1f), new Point(12, 52), new Point(15, 51));
                    }

                    if (_Value > 9)
                    {
                        g.DrawString(_Value.ToString(), frontSmallerFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(19, 26));
                    }
                    else
                    {
                        g.DrawString(_Value.ToString(), frontFirstDigitFont, new SolidBrush(DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor()), new PointF(24, 21));
                    }
                    Matrix matrix = new Matrix();
                    matrix.Rotate(-41);
                    g.Transform = matrix;
                    if (int.Parse(SelectSideToTheUpperRight(_Value)) > 9)
                    {
                        g.DrawString(SelectSideToTheUpperRight(_Value), smallerSideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(21, 35));
                    }
                    else
                    {
                        g.DrawString(SelectSideToTheUpperRight(_Value), sideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(21, 35));
                    }
                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(63);
                    g.Transform = matrix;
                    if (int.Parse(SelectSideToTheUpperLeft(_Value)) > 9)
                    {
                        g.DrawString(SelectSideToTheUpperLeft(_Value), smallerSideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(15, -20));
                    }
                    else
                    {
                        g.DrawString(SelectSideToTheUpperLeft(_Value), sideFont, new SolidBrush(Color.FromArgb(100, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(15, -20));
                    }

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(-104);
                    g.Transform = matrix;
                    if (int.Parse(SelectSideBelow(_Value)) > 9)
                    {
                        g.DrawString(SelectSideBelow(_Value), smallerBottomFont, new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-76, 14));
                    }
                    else
                    {
                        g.DrawString(SelectSideBelow(_Value), bottomFont, new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-76, 14));
                    }

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(24);
                    g.Transform = matrix;
                    if (int.Parse(SelectSideToTheLowerRight(_Value)) > 9)
                    {
                        g.DrawString(SelectSideToTheLowerRight(_Value), smallerSideFont, new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(66, 7));
                    }
                    else
                    {
                        g.DrawString(SelectSideToTheLowerRight(_Value), sideFont, new SolidBrush(Color.FromArgb(80, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(66, 7));
                    }

                    matrix.Reset();
                    g.ResetTransform();

                    matrix.Rotate(-23);
                    g.Transform = matrix;
                    if (int.Parse(SelectSideToTheLowerLeft(_Value)) > 9)
                    {
                        g.DrawString(SelectSideToTheLowerLeft(_Value), smallerSideFont, new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-15, 37));
                    }
                    else
                    {
                        g.DrawString(SelectSideToTheLowerLeft(_Value), sideFont, new SolidBrush(Color.FromArgb(60, DieRoller.Instance.DiceCollection.CurrentSet.PipColorAsColor())), new PointF(-15, 37));
                    }
                }
                return i;
            }
        }
        #endregion
        #region Sides
        public int Sides
        {
            get
            {
                return 12;
            }
        }
        #endregion
        #region Value
        private int _Value = 1;
        public int Value
        {
            get
            {
                return _Value;
            }
        }
        #endregion
        #region Roll
        public void Roll()
        {
            _Value = Randomizer.Instance.NextInt(1, Sides);
        }
        #endregion
        #endregion
        private string SelectSideToTheUpperLeft(int value)
        {
            switch (value)
            {
                case 1:
                    return "4";
                case 2:
                    return "7";
                case 3:
                    return "7";
                case 4:
                    return "10";
                case 5:
                    return "2";
                case 6:
                    return "7";
                case 7:
                    return "12";
                case 8:
                    return "6";
                case 9:
                    return "7";
                case 10:
                    return "7";
                case 11:
                    return "10";
            }
            return "4";
        }
        private string SelectSideToTheLowerLeft(int value)
        {
            switch (value)
            {
                case 1:
                    return "9";
                case 2:
                    return "1";
                case 3:
                    return "12";
                case 4:
                    return "11";
                case 5:
                    return "10";
                case 6:
                    return "2";
                case 7:
                    return "10";
                case 8:
                    return "3";
                case 9:
                    return "6";
                case 10:
                    return "9";
                case 11:
                    return "1";
            }
            return "7";
        }
        private string SelectSideToTheUpperRight(int value)
        {
            switch (value)
            {
                case 1:
                    return "7";
                case 2:
                    return "8";
                case 3:
                    return "6";
                case 4:
                    return "5";
                case 5:
                    return "6";
                case 6:
                    return "9";
                case 7:
                    return "9";
                case 8:
                    return "9";
                case 9:
                    return "3";
                case 10:
                    return "5";
                case 11:
                    return "4";
            }
            return "8";
        }
        private string SelectSideToTheLowerRight(int value)
        {
            switch (value)
            {
                case 1:
                    return "2";
                case 2:
                    return "5";
                case 3:
                    return "8";
                case 4:
                    return "8";
                case 5:
                    return "8";
                case 6:
                    return "8";
                case 7:
                    return "6";
                case 8:
                    return "11";
                case 9:
                    return "11";
                case 10:
                    return "4";
                case 11:
                    return "8";
            }
            return "3";
        }
        private string SelectSideBelow(int value)
        {
            switch (value)
            {
                case 1:
                    return "6";
                case 2:
                    return "6";
                case 3:
                    return "4";
                case 4:
                    return "12";
                case 5:
                    return "4";
                case 6:
                    return "5";
                case 7:
                    return "2";
                case 8:
                    return "4";
                case 9:
                    return "8";
                case 10:
                    return "11";
                case 11:
                    return "4";
            }
            return "9";
        }
    }
}